﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eCRAM.Dao;
using Microsoft.Practices.Unity;
using eCRAM.Model;

namespace eCRAM.Bll
{
    public static class CramManager 
    {

        public static Cram FindCram(Guid userId, int month, int year)
        {
            return CramRepository.FindCram(userId, month, year);
        }

        public static IEnumerable<Cram> FindCramsByUser(Guid userId)
        {
            return CramRepository.FindCramsByUserId(userId);
        }

        public static Cram FindCramByCramLine(Guid cramLineId)
        {
            return CramRepository.GetById(CramLineRepository.GetById(cramLineId).CramId);
        }

        public static IEnumerable<CramLine> GetCramLines(Cram cram)
        {
           return CramLineRepository.GetByCramId(cram.Id);
        }

        public static IEnumerable<Cram_Attachment> GetCramAttachments(Cram cram)
        {
            return CramAttachmentRepository.GetByCramId(cram.Id);
        }

        public static IEnumerable<Cram_Attachment> GetCramAttachmentsByCramId(Guid cramId)
        {
            return CramAttachmentRepository.GetByCramId(cramId);
        }

        public static IEnumerable<Cram_DailyActivity> GetDailyActivities(Guid cramLineId)
        {
            return CramDailyActivityRepository.GetByCramLineId(cramLineId);
        }

        public static IEnumerable<Cram_DailyActivity> GetDailyActivities(CramLine cramLine)
        {
           return CramDailyActivityRepository.GetByCramLineId(cramLine.Id);
        }


        public static void InsertCram(Cram cram)
        {
            CramRepository.Create(cram);
        }

        public static void InsertCramLine(CramLine cramLine)
        {
            CramLineRepository.Create(cramLine);
        }

        public static void InsertCramLines(IEnumerable<CramLine> cramLines)
        {
            CramLineRepository.InsertCramLines(cramLines);
        }

        public static void InsertDailyActivity(Cram_DailyActivity activity)
        {
            CramDailyActivityRepository.Create(activity);
        }

        public static void InsertDailyActivities(IEnumerable<Cram_DailyActivity> activities)
        {
            CramDailyActivityRepository.InsertActivities(activities);
        }

        public static void InsertAttachment(Cram_Attachment attachment)
        {
            CramAttachmentRepository.Create(attachment);
        }

        public static void InsertAttachments(IEnumerable<Cram_Attachment> attachments)
        {
            CramAttachmentRepository.InsertAttachments(attachments);
        }



        public static void DeleteCramLine(CramLine cramLine)
        {
            CramLineRepository.Delete(cramLine);
        }

        public static void DeleteCramLines(Cram cram)
        {
            CramLineRepository.DeleteByCramId(cram.Id);
        }

        public static void DeleteAttachment(Cram_Attachment attachment)
        {
            CramAttachmentRepository.Delete(attachment);
        }

        public static void DeleteAttachments(Cram cram)
        {
            CramAttachmentRepository.DeleteByCramId(cram.Id);
        }



        public static void DeleteDailyActiviy(Cram_DailyActivity activity)
        {
            CramDailyActivityRepository.Delete(activity);
        }

        public static void DeleteDailyActivities(CramLine cramLine)
        {
            CramDailyActivityRepository.DeleteActivities(cramLine.Id);
        }

        public static void DeleteDailyActivities(Guid cramLineId)
        {
            CramDailyActivityRepository.DeleteActivities(cramLineId);
        }


        public static Cram GetCramById(Guid idCram)
        {
            return CramRepository.GetById(idCram);
        }

        public static Dictionary<int, decimal> GetMonthRates(Cram cram, CramLine cramLine)
        {
            // Nombre de jours dans le mois
            int nbDayMonth = DateTime.DaysInMonth(cram.Year, cram.Month);
            // liste des jours travaillés
            var listWorkedDays = cramLine.Cram_DailyActivities;

            var listRetour = new Dictionary<int, decimal>(nbDayMonth);

            for (var i = 1; i <= nbDayMonth; i++)
            {
                listRetour.Add(i,
                    listWorkedDays.Where(da => da.Day.Day == i).Select(da => da.Value).FirstOrDefault());
            }

            return listRetour;
        }

        public static Cram_Attachment GetAttachmentByCramIdAndName(Guid idCram, string attachmentName)
        {
            return CramRepository.GetAttachmentByCramIdAndName(idCram, attachmentName);
        }
    }
}
